package cn.jdy.plugins.wap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Search;
import cn.jdy.plugins.service.SearchService;

/**
 * wap 搜索 现仅用于汉水商城首页搜索
 * 
 * @author yym
 * @since 28/12/2015
 */
@Controller
@RequestMapping("/search")
public class SearchCtrl {
	@Autowired
	private SearchService searchService;

	/**
	 * 汉水商城首页搜索，NHPM：新闻帮助产品商家综合查询
	 * 
	 * @param search
	 * @param keywords
	 * @return
	 */
	@RequestMapping("/")
	public ModelAndView searchby(Search search, String keywords) {
		ModelAndView mv = new ModelAndView("/searchList.jsp");
		try {
			// 设置分页信息
			if (search.getPagesize() == 0)
				search.setPagesize(10); // 默认长度10
			if (search.getPageNo() <= 0)
				search.setPageNo(1); // 默认页数1
			search.setPagestart((search.getPageNo() - 1) * search.getPagesize()); // 显示起始索引
			mv.addObject("dataList",
					searchService.queryByLikeNHPM(search, keywords));
			mv.addObject("total", searchService.queryCountNHPM(keywords));
			mv.addObject("keywords", keywords);
			mv.addObject("pageNo", search.getPageNo());
			mv.addObject("size", search.getPagesize());
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "搜索异常", e);
		}
		return mv;
	}
}
